package fr.isen.smssender.sms;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import fr.isen.smssender.R;
import fr.isen.smssender.database.DBManager;
import fr.isen.smssender.database.SmsSenderSQLite;

public class SmsListActivity extends Activity {

    public DBManager myDBManager;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_list);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.smsList);
        ListView listView = new ListView(this);
        linearLayout.addView(listView);


        myDBManager = new DBManager(this);

        String[] from = {SmsSenderSQLite.COL_MESSAGE};
        int[] to = {R.id.listitem};

        Cursor cursor = myDBManager.getSmsCursor();

        listView.setAdapter(new SimpleCursorAdapter(this, R.layout.sms_list_item, cursor, from,to));


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                newActivity(position);
            }
        });

        final Button button = (Button) findViewById(R.id.buttonAddSms);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                newActivity(-1);
            }
        });
    }



    public void refresh()
    {
        Intent i = new Intent(this, SmsListActivity.class);
        startActivity(i);
    }

    public void newActivity(int position)
    {
        int id= myDBManager.getSmsIdByPositionInLV(position);
        Intent j = new Intent(this, SmsParameterActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id",id);
        j.putExtras(bundle);
        startActivity(j);
    }



}
